package artexercise;

public class ArithmeticExpressionTest {
    public void testAdditionExpression() {
        System.out.println("测试加法算式...");
        // 测试加法算式
        AdditionExpression add = new AdditionExpression(5, 3);
        assert add.getResult() == 8 : "加法计算错误";
        assert "+".equals(add.getOperator()) : "运算符错误";
        assert add.isValid() : "有效性检查错误";
        System.out.println("✓ 加法测试通过");
    }
    public void testSubtractionExpression() {
        System.out.println("测试减法算式...");
        // 测试减法算式
        SubtractionExpression sub = new SubtractionExpression(10, 4);
        assert sub.getResult() == 6 : "减法计算错误";
        assert "-".equals(sub.getOperator()) : "运算符错误";
        assert sub.isValid() : "有效性检查错误";
        // 测试无效的减法（结果为负）
        SubtractionExpression invalidSub = new SubtractionExpression(3, 5);
        assert !invalidSub.isValid() : "负数结果应该无效";
        System.out.println("✓ 减法测试通过");
    }
    public void testExerciseGeneration() {
        System.out.println("测试习题生成...");
        Exercise exercise = new Exercise();
        // 测试加法生成
        exercise.generateAdditionExercises(10);
        assert exercise.getExerciseCount() == 10 : "习题数量错误";
        // 验证所有加法题都是有效的
        for (int i = 0; i < 10; i++) {
            ArithmeticExpression expr = exercise.getExpression(i);
            assert expr instanceof AdditionExpression : "应该是加法题";
            assert expr.isValid() : "题目应该有效";
        }

        exercise.clear();

        // 测试减法生成
        exercise.generateSubtractionExercises(5);
        assert exercise.getExerciseCount() == 5 : "习题数量错误";

        // 验证所有减法题都是有效的（结果不为负）
        for (int i = 0; i < 5; i++) {
            ArithmeticExpression expr = exercise.getExpression(i);
            assert expr instanceof SubtractionExpression : "应该是减法题";
            assert expr.isValid() : "题目应该有效";
            assert expr.getResult() >= 0 : "减法结果应该非负";
        }

        System.out.println("✓ 习题生成测试通过");
    }

    public void testMixedExerciseGeneration() {
        System.out.println("测试混合习题生成...");

        Exercise exercise = new Exercise();

        // 测试混合生成
        exercise.generateMixedExercises(20);
        assert exercise.getExerciseCount() == 20 : "习题数量错误";

        int additionCount = 0;
        int subtractionCount = 0;

        for (int i = 0; i < 20; i++) {
            ArithmeticExpression expr = exercise.getExpression(i);
            assert expr.isValid() : "题目应该有效";

            if (expr instanceof AdditionExpression) {
                additionCount++;
            } else if (expr instanceof SubtractionExpression) {
                subtractionCount++;
            }
        }

        // 验证确实有加法和减法混合
        assert additionCount > 0 : "应该有加法题";
        assert subtractionCount > 0 : "应该有减法题";
        assert (additionCount + subtractionCount) == 20 : "题目总数应该为20";

        System.out.println("✓ 混合习题测试通过");
    }

    public void testFullExerciseSet() {
        System.out.println("测试完整习题集...");

        Exercise exercise = new Exercise();

        // 生成完整的150道题
        exercise.generateAdditionExercises(50);
        exercise.generateSubtractionExercises(50);
        exercise.generateMixedExercises(50);

        assert exercise.getExerciseCount() == 150 : "习题总数应该为150";

        // 验证所有题目都有效
        for (int i = 0; i < 150; i++) {
            ArithmeticExpression expr = exercise.getExpression(i);
            assert expr != null : "题目不应该为null";
            assert expr.isValid() : "所有题目都应该有效";
        }

        System.out.println("✓ 完整习题集测试通过");
    }
}